﻿/**
 * ...
 * @author Sergey Mamyan
 */

package tx.squirrel.neko.admin.handler;

import neko.Lib;
import neko.Web;
import tx.facebook.core.Facebook;
import tx.facebook.methods.AdminMethods;

import tx.squirrel.neko.admin.model.Application;
import tx.squirrel.neko.core.handler.BaseHandler;

import tx.squirrel.neko.core.model.Session;

class ApplicationHandler extends BaseHandler {
/*
	public function new() {
		super();
		logged("default", 	"admin_application.mtt",			doDefault	);
		logged("edit", 		"admin_application_modify.mtt", 	doEdit		);
		logged("show", 		"admin_application_modify.mtt", 	doShow		);
		logged("add", 		"admin_application_modify.mtt", 	doAdd		);
		logged("update",	null, 								doUpdate	);
	}
	
	public function doDefault():Void {
		display(Application.manager.all());
	}
	
	public function doShow():Void {
		var appId:Int = Std.parseInt(argument(1));
		var app:Application = Application.manager.get(appId);
		if (app != null) {
			display({
				application:app,
				action:"show"
			});
		}else {
			Session.writeLog("error","Application with id <"+argument(1)+"> unavailable.");
			redirect("/admin/application");
		}
	}
	
	public function doAdd():Void {
		var app:Application = new Application();
		if (Web.getMethod() == "POST") {
			var post:Hash<String> = Web.getParams();
			app.apiKey 		= post.get("apiKey");
			app.apiSecret 	= post.get("apiSecret");
			app.insert();
			Session.writeLog("info","Application <"+app.applicationName+"> successfuly added");
			redirect("/admin/application");
		}else{
			display({
				application:app,
				action:"add"
			});
		}
	}
	
	public function doEdit():Void {
		var appId:Int = Std.parseInt(argument(1));
		var app:Application = Application.manager.get(appId);
		if (app != null) {
			if (Web.getMethod() == "POST") {
				var post:Hash<String> = Web.getParams();
				app.apiKey 		= post.get("apiKey");
				app.apiSecret 	= post.get("apiSecret");
				app.update();
				Session.writeLog("info","Application <"+app.applicationName+"> successfuly updated");
				redirect("/admin/application");
			}else{
				display({
					application:app,
					action:"add"
				});
			}
		}else{
			Session.writeLog("error","User with id <"+argument(1)+"> unavailable.");
			redirect("/admin/application");
		}
	}
	
	public function doUpdate():Void {
		var appId:Int = Std.parseInt(argument(1));
		var app:Application = Application.manager.get(appId);
		if (app != null) {
			app.update();
			Session.writeLog("info","Application <"+app.applicationName+"> successfuly updated");
			redirect("/admin/application");
		}else{
			Session.writeLog("error","User with id <"+argument(1)+"> unavailable.");
			redirect("/admin/application");
		}
	}
	*/
}